package com.yanqu.road.dao.impl.activity.xiaowuRecall;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiaowuRecallUserDataDaoImpl extends TempDataObjectDao<XiaowuRecallUserData> {
    public static String getXiaowuRecallUserDataSql = "select * from `t_u_activity_xiaowu_recall` where `activityId`=? and `userId`=?";

    public XiaowuRecallUserData getXiaowuRecallUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiaowuRecallUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateXiaowuRecallUserDataSql = "update `t_u_activity_xiaowu_recall` set sliceCondition=?,score=?,commonTimes=?,soul=?,page=?,getRewardTimes=?,turnCost=?,turnScore=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(XiaowuRecallUserData xiaowuRecallUserData) {
        xiaowuRecallUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaowuRecallUserData.getSliceCondition())));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getSoul()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getPage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaowuRecallUserData.getAwarded())));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getTurnCost()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getTurnScore()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getUserId()));
        if (getDb().executeNoneQuery(updateXiaowuRecallUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiaowuRecallUserDataSql = "insert into `t_u_activity_xiaowu_recall`(activityId,userId,sliceCondition,score,commonTimes,soul,page,getRewardTimes,turnCost,turnScore) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(XiaowuRecallUserData xiaowuRecallUserData) {
        xiaowuRecallUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaowuRecallUserData.getSliceCondition())));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getCommonTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getSoul()));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getPage()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiaowuRecallUserData.getAwarded())));
        para.put(i++, new DbParameter(Types.INTEGER, xiaowuRecallUserData.getTurnCost()));
        para.put(i++, new DbParameter(Types.BIGINT, xiaowuRecallUserData.getTurnScore()));
        if (getDb().executeNoneQuery(insertXiaowuRecallUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    public static String getAutoRewardUserDataSql = "select * from `t_u_activity_xiaowu_recall` where `activityId`=? ";

    public Map<Long, XiaowuRecallUserData> getAutoRewardUserDataMap(int activityId) {
        Map<Long, XiaowuRecallUserData> userDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAutoRewardUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiaowuRecallUserData parse = parse(rs);
                userDataMap.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userDataMap;
    }

    @Override
    protected XiaowuRecallUserData parseData(ResultSet rs) throws SQLException {
        XiaowuRecallUserData xiaowuRecallUserData = new XiaowuRecallUserData();
        xiaowuRecallUserData.setActivityId(rs.getInt("activityId"));
        xiaowuRecallUserData.setUserId(rs.getLong("userId"));
        xiaowuRecallUserData.setSliceCondition(JSON.parseObject(rs.getString("sliceCondition"), int[].class));
        xiaowuRecallUserData.setScore(rs.getLong("score"));
        xiaowuRecallUserData.setCommonTimes(rs.getInt("commonTimes"));
        xiaowuRecallUserData.setSoul(rs.getInt("soul"));
        xiaowuRecallUserData.setPage(rs.getInt("page"));
        xiaowuRecallUserData.setAwarded(JSON.parseObject(rs.getString("getRewardTimes"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {
        }));
        xiaowuRecallUserData.setTurnCost(rs.getInt("turnCost"));
        xiaowuRecallUserData.setTurnScore(rs.getLong("turnScore"));
        return xiaowuRecallUserData;
    }

}
